package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.dto.UserDto;
import com.qk.entity.User;
import com.qk.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RequestMapping("/users")
@RestController
public class UserController {

    @Autowired
    private UserService userService;
    //根据接口文档,用对应参数接收前端传的请求,(保证接口文档名与形参名一样)
    @GetMapping
    public Result list(UserDto userDto){
        log.info("查询用户列表");
        //调用service接口方法
        PageResult<User> page =userService.list(userDto);
        return Result.success(page);
    }
    @DeleteMapping ("/{ids}")
    public Result deleteUser(@PathVariable ("ids")List<Integer> ids){
        log.info("删除用户");
        userService.deleteUser(ids);
        return Result.success();
    }
    @PostMapping
    public Result addUser( @RequestBody User user){
        log.info("添加用户");
        userService.addUser(user);
        return Result.success();
    }
    @GetMapping ("/{id}")
    public Result getUserById(@PathVariable("id")Integer id){
        log.info("根据查询用户");
        User user = userService.getUserById(id);
        return Result.success(user);
    }
    @PutMapping
    public Result updateUser(@RequestBody User user){
        log.info("更新用户");
        userService.updateUser(user);
        return Result.success();
    }
    @GetMapping("/list")
    public Result getAll(){
        log.info("查询所有用户");
        return Result.success(userService.getAll());
    }
    @GetMapping("/role/{roleLabel}")
    public Result getUserByRole(@PathVariable("roleLabel") String roleLabel){
        log.info("根据角色查询用户");
        return Result.success(userService.getUserByRole(roleLabel));
    }
    @GetMapping("/dept/{deptId}")
    public Result getUserByDept(@PathVariable("deptId") Integer deptId){
        log.info("根据部门查询用户");
        return Result.success(userService.getUserByDept(deptId));
    }

}
